<?php
require_once "BaseAction.php";
class Event_BrowseController extends TTEvent_Base_Action 
{
	
	public function init(){
		parent::init();
		$this->_helper->layout->setLayout('event');
	}
	
	public function catlistAction(){
		$this->_helper->eventCommon->buildMainMenuBlock(1);
		
		$user = $this->getUser();
		$req = $this->getRequest();
		$cid = $this->_getParam('cid');  
		$page = $req->getQuery('page');
		
		$term = $this->_helper->modelLoader->getTermInfo($cid);
		$this->view->termName = "Tất cả";
		if(!empty($term)){
			$this->view->termName = $term->name;
			$this->setTitle('Chuyên mục sự kiện '.$term->name);
		}
		$this->_helper->eventCommon->buildHtmlEventLeftMenu('cat', $user->uid, 0, $cid, false);
		$req = $this->getRequest();
		$p = (int)$req->getQuery('p', 0);
		$city = (int)$req->getQuery('c', 0);
		
		//-----------------
		$city1 = new stdClass();
		$city2 = new stdClass();
		$city3 = new stdClass();
		$city1->cid=101; $city1->name="Hà Nội";
		$city2->cid=701; $city2->name="Tp. Hồ Chí Minh";
		$city3->cid=501; $city3->name="Đà Nẵng";
		$cities = array();
		$cities[] = $city1;
		$cities[] = $city2;
		$cities[] = $city3;
		
	//	$searchCond = $this->makeSearchCond();
		$this->view->city = $city;
		$this->view->promotedCities = $cities;
	//	$this->view->htmlSearchBox = $this->buildSearchBox($searchCond);
		$this->view->htmlEventList = $this->buildCatListBox($cid, $p, $city, $page,$term->tid);
		$this->view->banners = $this->_helper->eventCommon->getBanner('category');
	}
	
	public function searchAction(){
		$this->_helper->eventCommon->buildMainMenuBlock(1);
		
		$user = $this->getUser();
		$req = $this->getRequest();
		$page = (int)$req->getQuery('page', 0);
		
		$this->view->termName = "Tìm kiếm sự kiện";		
		
		$searchCond = $this->makeSearchCond();
		$this->view->htmlEventList = $this->buildSearchListBox($searchCond, $page);
		$this->view->htmlSearchBox = $this->buildSearchBox($searchCond);
		$this->view->banners = $this->_helper->eventCommon->getBanner('category');
		                     
		$this->_helper->eventCommon->buildHtmlEventLeftMenu('search', $user->uid, 0, 0, false);
      /*  $testModel = $this->loadModel('test');   
        $network=$testModel->getAllNetwork();
        $this->view->list_nw = $this->view->partial('list_nw.phtml',$network);     */
		//$this->render('catlist');
	}
    
    
 /*   public function weddingAction(){
        
        require_once 'lib/thrift/client/PhotoService.php';
        $this->_helper->eventCommon->buildMainMenuBlock(2);
        
        $user = $this->getUser();
        $req = $this->getRequest();
        
        $this->view->termName = "Đám cưới của tôi";
        
        
        $searchCond = $this->makeSearchCond();
        $currendWeddingEid = 1345;
        
        // get current wedding 
        $currentWedding = $this->_helper->modelLoader->getEventInfo($currendWeddingEid);
        
        // get current wedding photo
        $cacheKey = "current_wedding_photo_".$currendWeddingEid;
        $currentWeddingPhotos = $this->getCache($cacheKey);
        if($currentWeddingPhotos === false){
            $p_client = null;
            $currentWeddingPhotos = array();
            try {
                $p_client = new PhotoService(PHOTO_THRIFT_SERVER, PHOTO_THRIFT_PORT);
                $p_client->OpenTransport();
                $total = $p_client->client()->getEventAlbumCount($currendWeddingEid);
                if($total > 0){
                    $currentWeddingPhotos = $p_client->client()->getEventAlbum($currendWeddingEid, 1, $total);
                }
                $p_client->CloseTransport();
            } catch (Exception $ex){
                //$photos = array();
            }
            $this->setCache($cacheKey, $currentWeddingPhotos);
        }
        
        $weddingList = array();
        $weddingEid = array(675, 1345);
        foreach($weddingEid as $weid){
            $wedding = array();
            $wedding['event'] = $this->_helper->modelLoader->getEventInfo($weid);
            $wedding['vote_info'] = $this->_helper->modelLoader->eventVoteInfo($weid,0,0,0);
            $weddingList[] = $wedding;
        }
        
        shuffle($currentWeddingPhotos);
        $this->view->currentWedding = $currentWedding;
        $this->view->currentWeddingPhotos =  $currentWeddingPhotos;
        $this->view->weddingList = $weddingList;
        
        //$this->view->htmlEventList = $this->buildSearchListBox($searchCond, $page);
        //$this->view->banners = $this->_helper->eventCommon->getBanner('category');
        
        $this->_helper->eventCommon->buildHtmlEventLeftMenu('search', $user->uid, 0, 0, false);
        //$this->render('catlist');
    }     */
	
	public function buildSearchListBox($searchCond, $page=0, $limit=20){
		
		$cache_key = md5(serialize($searchCond))."_".$page;
		
	//	$data = $this->getCache($cache_key);
		
		if(!$data){
			$eventModel = $this->loadModel('Event');
			$eventList = $eventModel->search($searchCond, $page, $limit);
			$pageInfo = $eventModel->getPageInfo();   
			$assign_list = array();
			$assign_list['event_list'] = $eventList;
			$assign_list['action'] = 'search';
			$assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => $this->searchCondToQuery($searchCond), "url" => "/event/search"));
			
			$data = $this->view->partial('event-list.phtml', $assign_list);
		//	$this->setCache($cache_key, $data);
		}
		return $data;
	}
	
	public function buildCatListBox($cid, $p, $c, $page=0,$tid){
		$page = (int)$page;
		$cache_key = "html_cat_".$cid."_".$p."_".$c."_".$page;
		$data = $this->getCache($cache_key);
		if(!$data){
			$eventModel = $this->loadModel('Event');
          /*  $eventList = $eventModel->getListByCategory($cid, $p, $c, $page);   */
		//va
        	$eventList = $eventModel->getListByCategory2($cid, $p, $c, $page);
		//va
        	$pageInfo = $eventModel->getPageInfo();
			
			$assign_list = array();
			$assign_list['event_list'] = $eventList;
			$assign_list['action'] = 'category';
			$assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => "", "url" => "/event/cat/".$cid));
			$assign_list['tid']=$tid;
			$data = $this->view->partial('event-list.phtml', $assign_list);
			$this->setCache($cache_key, $data);
		}
		return $data;
	}
	
	private function buildSearchBox($searchCond){
		$user = $this->getUser();
		//$networkList = $this->_helper->network->getUserNetworks($user->uid);
		//$networkList = $this->_helper->Network->getAllNetworks();
		$categoryList = $this->_helper->eventCommon->getEventCategories();
		
		return $this->_helper->eventCommon->buildHtmlSearchBox($searchCond, $categoryList);
	}
	
	private function makeSearchCond(){
		$searchCond = array();
		
		$req = $this->getRequest();
		if($req->isPost()){
			$searchCond['keyword'] = $req->getPost('keyword', "");
			$searchCond['nwid'] = (int)$req->getPost('nwid', "");
			$searchCond['cid'] = (int)$req->getPost('cid', "");
			$searchCond['start_time'] = $req->getPost('start_time', "");
			$searchCond['end_time'] = $req->getPost('end_time', "");
		}
		else{
			$searchCond['keyword'] = $req->getQuery('keyword', "");
			$searchCond['nwid'] = $req->getQuery('nwid', 0);
			$searchCond['cid'] = $this->_getParam('cid', 0);
			$searchCond['start_time'] = $req->getQuery('start_time', "");
			$searchCond['end_time'] = $req->getQuery('end_time', "");
		}
		$searchCond['p'] = (int)$req->getPost('p', 0);
		if(!$searchCond['p']){
			$searchCond['p'] = (int)$req->getQuery('p', 0);
		}
		
		return $searchCond;
	}
	private function searchCondToQuery($searchCond){
		$ret = "";
		if(is_array($searchCond) && count($searchCond) >0){
			
			foreach ($searchCond as $key=>$val){
				if(!empty($val))
					$ret .= ($ret!=""?"&":"")."$key=$val";
			}
		}
		return $ret;
	}
}

